// 模型
const { User, RouteList, Routers } = require('../models/user');
const { Project, Activity } = require('../models/workplace');
const { List } = require('../models/list');
const { NewsModel } = require("../models/news");
const { TableModel, TableData, DialogItem, Devices, Factory, DeviceTypes, Levels } = require("../models/table")
// 数据
const user = require('./user/index.json');
const routeList = require('./user/index.json');
const routers = require('./user/index.json');
const project = require('./workplace/index.json');
const activity = require('./workplace/index.json');
const list = require('./list/index.json');
const newsModel = require("./news/index.json");
const tableModel = require("./table/index.json");
const tableData = require("./table/index.json");
const dialogItem = require("./table/index.json");
const devices = require("./table/index.json");
const factory = require("./table/index.json");
const deviceTypes = require("./table/index.json");
const levels = require("./table/index.json");

async function insertData() {
    // 所有数据放在json中，通过遍历数组生成模型
    const mapJsonKeyToClass = {
        User,
        RouteList,
        Project,
        Activity,
        List,
        Routers,
        NewsModel,
        TableModel,
        TableData,DialogItem, Devices, Factory, DeviceTypes, Levels
    };

    // 多个数据合并
    const json = Object.assign(
        user,
        routeList,
        project,
        activity,
        list,
        routers,
        newsModel,
        tableModel,dialogItem,devices,factory,deviceTypes,levels
    );

    //将数据插入到模型中
    const promiseArray = Object.keys(json).reduce(
        (r, e) =>
            json[e].map(obj => {
                return mapJsonKeyToClass[e].create(obj);
            }),
        []
    );

    return Promise.all(promiseArray);
}

module.exports = {
    insertData
};
